﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyDMS.BusinessLayer;

public partial class MyProjects : PageBaseClass
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<Project> lstProjManaged = new ProjectFactory().GetAllBy(Project.ProjectFields.Assigned_To, _author.user_Code);
            gvProjectsManaged.DataSource = lstProjManaged;
            gvProjectsManaged.DataBind();

            object dataObject;
            new SqlFactory().ExecuteQuery("select p.* from Project p " +
                " inner join UserInProject uip on p.Project_Code = uip.Project_Code " +
                " where uip.User_Code = " + _author.user_Code, out dataObject, SqlFactory.DataObjectType.DataSet);

            if (dataObject != null)
            {
                gvProjectsOther.DataSource = dataObject;
                gvProjectsOther.DataBind();
            }
        }
    }

    private void BindTheProjectUsers(int projectCode)
    {
        object dataObject = null;
        new SqlFactory().ExecuteQuery("select u.User_Code, FirstName + ' '+ LastName as [Full_Name], Email_Address as Email  from userinproject up " +
            " inner join userinfo u on u.user_code = up.user_code " +
            " where  up.Project_Code = " + projectCode, out dataObject, SqlFactory.DataObjectType.DataSet);

        if (dataObject != null)
        {
            this.ddlUsers.DataSource = dataObject;
            this.ddlUsers.DataTextField = "Full_Name";
            this.ddlUsers.DataValueField = "User_Code";
            this.ddlUsers.DataBind();
        }
    }

    protected void btnBindUsers_Click(object sender, EventArgs e)
    {
        BindTheProjectUsers(int.Parse(hdfItemCode.Value));
    }

    protected void btnAddTask_Click(object sender, EventArgs e)
    {
        ProjectTask ptask = new ProjectTask();
        ptask.Task_Title = txtTaskTitle.Text;
        ptask.Task_Desc = txtTastDesc.Text;
        ptask.Assigned_To = int.Parse(ddlUsers.SelectedValue);
        ptask.Open_By = _author.user_Code;
        ptask.Open_Date = DateTime.Parse(txtTaskOpenDate.Text);
        ptask.Project_Code = int.Parse(hdfItemCode.Value);
        ptask.DeadLine = DateTime.Parse(txtTaskDeadline.Text);

        if (new ProjectTaskFactory().Insert(ptask))
        {
            lblStatus.Text = "Assign to " + ddlUsers.SelectedItem.Text;
        }
        else
        {
            lblStatus.Text = "Not Assign to " + ddlUsers.SelectedItem.Text;
        }
    }
}

